package com.liang.paul.officelite.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.dd.processbutton.iml.ActionProcessButton;
import com.liang.paul.officelite.R;
import com.liang.paul.officelite.beams.OfficeUser;
import com.liang.paul.officelite.beams.TeamBeam;
import com.liang.paul.officelite.beams.TeamPersonMapBeam;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.SaveListener;

public class AddTeamActivity extends FastActivity {

    ActionProcessButton btn_add;
    EditText teamId;
    public static final int ADDTEAM = 103;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_team);
        initialView();
    }

    private void initialView(){
        btn_add = f(R.id.btn_addteam_add);
        teamId = f(R.id.editText_addteam_teamId);

        btn_add.setMode(ActionProcessButton.Mode.ENDLESS);

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_add.setProgress(1);
                BmobQuery<TeamBeam> query = new BmobQuery<TeamBeam>();
                query.getObject(getApplicationContext(), teamId.getText().toString(), new GetListener<TeamBeam>() {
                    @Override
                    public void onSuccess(TeamBeam teamBeam) {
                        TeamPersonMapBeam map = new TeamPersonMapBeam();
                        map.setPersonId(BmobUser.getCurrentUser(getApplicationContext(), OfficeUser.class).getObjectId());
                        map.setTeamId(teamBeam.getObjectId());
                        map.save(getApplicationContext(), new SaveListener() {
                            @Override
                            public void onSuccess() {
                                Intent intent = new Intent();
                                intent.putExtra("isSuccess", true);
                                setResult(ADDTEAM, intent);
                                finish();
                            }

                            @Override
                            public void onFailure(int i, String s) {
                                btn_add.setProgress(0);
                                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        btn_add.setProgress(0);
                        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
